package com.travel.modules.review.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.commons.annotation.SysLog;
import com.travel.commons.shiro.ShiroUserGetter;
import com.travel.commons.utils.R;
import com.travel.commons.validator.ValidatorUtils;
import com.travel.commons.validator.groups.AddGroup;
import com.travel.modules.review.entity.Feedback;
import com.travel.modules.review.service.FeedbackService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 反馈功能
 */
@RestController
@RequestMapping("/feedback")
public class FeedbackController {
    @Resource
    private FeedbackService feedbackService;

    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> param) {
        Integer status = (Integer) param.get("status");
        if (status == null) {
            return R.ok().put("page", new ArrayList<>());
        }
        if (status != 1 && status != 0) {
            return R.error("参数错误");
        }

        List<Feedback> list = feedbackService.getFeedbackList(status);
        return R.ok().put("page", list);
    }

    @RequestMapping("/list/current")
    public R list2Current() {
        List<Feedback> list = feedbackService.list(new QueryWrapper<Feedback>().eq("user_id", ShiroUserGetter.getUserId()));
        return R.ok().put("page", list);
    }

    @SysLog("反馈提交")
    @RequestMapping("/submit")
    public R submit(@RequestBody Feedback form) {
        form.setUserId(ShiroUserGetter.getUserId());
        form.setStatus(0);
        ValidatorUtils.validate(form, AddGroup.class);
        feedbackService.save(form);
        return R.ok();
    }

    @SysLog("反馈标记处理")
    @RequestMapping("/status")
    public R status(@RequestBody Feedback form) {
        Integer id = form.getId();
        if (id == null) {
            return R.error("缺少id");
        }
        Feedback old = feedbackService.getById(id);
        old.setStatus(1);
        feedbackService.updateById(old);
        return R.ok();
    }

    @SysLog("反馈删除")
    @RequestMapping("/del")
    public R del(@RequestBody Feedback form) {
        Integer id = form.getId();
        if (id == null) {
            return R.error("缺少id");
        }
        feedbackService.removeById(id);
        return R.ok();
    }
}
